
package guoan.web.webCustomer;

import guoan.core.dto.DataGridModel;
import guoan.core.dto.PageResponse;
import guoan.core.dto.RestResponse;
import guoan.domain.AutoForm;
import guoan.domain.AutoFormInput;
import guoan.service.AutoFormInputService;
import guoan.service.AutoFormService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.base.Preconditions;

@Controller
@RequestMapping("/webCustomer/autoForm")
public class AutoFormController {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private AutoFormService autoFormService;

    @Autowired
    private AutoFormInputService autoFormInputService;
    
    @Value("${weixin.host}")
	private String host;

    /** 跳转到考试主页 */
    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index( Model model){
    	model.addAttribute("host", "http://"+host+"/");
        return "/webCustomer/autoForm/index";
    }

    @RequestMapping(value = "data", method = RequestMethod.POST)
    @ResponseBody
    public PageResponse<AutoForm> data(DataGridModel dataGridModel){
        return autoFormService.page(dataGridModel);
    }

    /** 查看 */
    @RequestMapping(value = { "update", "add" }, method = RequestMethod.GET)
    public String toUpdate(Long id, Model model){
        if (id != null) {
            AutoForm autoForm = autoFormService.findOne(id);
            model.addAttribute("autoForm", autoForm);
        }
        return "/webCustomer/autoForm/update";
    }

    /** 保存 */
    @RequestMapping(value = { "update", "add" }, method = RequestMethod.POST)
    @ResponseBody
    public RestResponse<Boolean> save(AutoForm autoForm){
        Preconditions.checkNotNull(autoForm, "参数错误");
        autoFormService.save(autoForm);
        return RestResponse.ok(true);
    }

    @RequestMapping(value = "del", method = RequestMethod.GET)
    @ResponseBody
    public RestResponse<Boolean> del(Long id){
        Preconditions.checkNotNull(id, "参数错误");
        autoFormService.del(id);
        return RestResponse.ok(true);
    }

    /** 跳转到考试主页 */
    @RequestMapping(value = "indexInput", method = RequestMethod.GET)
    public String indexInput(Long autoFormId, Model model){
        Preconditions.checkNotNull(autoFormId, "参数错误");
        List<String> formInputNames = autoFormService
                .findFormInputNames(autoFormId);
        AutoForm autoForm = autoFormService.findOne(autoFormId);
        Preconditions.checkNotNull(autoForm, "参数错误");
        model.addAttribute("formInputNames", formInputNames);
        model.addAttribute("autoForm", autoForm);
        return "/webCustomer/autoForm/input/index";
    }

    @RequestMapping(value = "dataInput", method = RequestMethod.POST)
    @ResponseBody
    public PageResponse<Object[]> data(DataGridModel dataGridModel,
            Long autoFormId){
        return autoFormService.page(autoFormId, dataGridModel);
    }

    /** 保存预约考试 */
    @RequestMapping(value = "showInput", method = RequestMethod.GET)
    public String show(String formid, Long autoFormId, Model model){
        Preconditions.checkNotNull(autoFormId, "参数错误");
        Preconditions.checkNotNull(formid, "参数错误");
        List<Object[]> form = autoFormService.findFormInfo(formid);
        AutoForm autoForm = autoFormService.findOne(autoFormId);
        model.addAttribute("autoForm", autoForm);
        model.addAttribute("forms", form);
        return "/webCustomer/autoForm/input/show";
    }

    /** 跳转到考试主页 */
    @RequestMapping(value = "item/index", method = RequestMethod.GET)
    public String itemIndex(Long autoFormId, Model model){
        AutoForm autoForm = autoFormService.findOne(autoFormId);
        model.addAttribute("autoForm", autoForm);
        return "/webCustomer/autoForm/item/index";
    }

    @RequestMapping(value = "item/data", method = RequestMethod.POST)
    @ResponseBody
    public PageResponse<AutoFormInput> itemData(Long autoFormId,
            DataGridModel dataGridModel){
        return autoFormInputService.page(autoFormId, dataGridModel);
    }

    /** 查看 */
    @RequestMapping(value = { "item/update", "item/add" },
            method = RequestMethod.GET)
    public String toItemUpdate(Long autoFormId, Long id, Model model){
        if (autoFormId != null) {
            AutoForm autoForm = autoFormService.findOne(autoFormId);
            model.addAttribute("autoForm", autoForm);
            if (id != null) {
                AutoFormInput autoFormInput = autoFormInputService.findOne(id);
                model.addAttribute("autoFormInput", autoFormInput);
            }
        }
        return "/webCustomer/autoForm/item/update";
    }

    /** 保存 */
    @RequestMapping(value = { "item/update", "item/add" },
            method = RequestMethod.POST)
    @ResponseBody
    public RestResponse<Boolean> itemSave(AutoFormInput autoFormInput){
        Preconditions.checkNotNull(autoFormInput, "参数错误");
        autoFormInputService.save(autoFormInput);
        return RestResponse.ok(true);
    }

    @RequestMapping(value = "item/del", method = RequestMethod.GET)
    @ResponseBody
    public RestResponse<Boolean> itemDel(Long id){
        Preconditions.checkNotNull(id, "参数错误");
        autoFormInputService.delete(id);
        return RestResponse.ok(true);
    }
    @RequestMapping(value = "dataInput/del", method = RequestMethod.GET)
    @ResponseBody
    public RestResponse<Boolean> dataInputDel(String formid){
        Preconditions.checkNotNull(formid, "参数错误");
        autoFormInputService.deleteByFormid(formid);
        return RestResponse.ok(true);
    }
}
